package com.cc.service.impl;

import com.cc.comm.util.DateUtil;
import com.cc.model.entity.PurchaseSettlement;
import com.cc.mapper.PurchaseSettlementMapper;
import com.cc.model.vo.SupplierOrderGroupVo;
import com.cc.service.PurchaseOrdersService;
import com.cc.service.PurchaseSettlementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 采购结算表 服务实现类
 * </p>
 *
 * @author 晓东√
 * @since 2025-09-19 11:18
 */
@Service
public class PurchaseSettlementServiceImpl
        extends ServiceImpl<PurchaseSettlementMapper, PurchaseSettlement>
        implements PurchaseSettlementService {
    @Autowired
    private PurchaseSettlementMapper purchaseSettlementMapper;
    @Autowired
    private PurchaseOrdersService purchaseOrdersService;

    @Override
    public void generateSettlement() {
        //1.查询采购订单表
        //2.根据供应商ID进行分组
        List<SupplierOrderGroupVo> supplierOrderGroup = purchaseOrdersService.supplierOrderGroup();
        //3.生成采购结算单
        supplierOrderGroup.forEach(supplierOrderGroupVo -> {
            PurchaseSettlement purchaseSettlement = new PurchaseSettlement();
            purchaseSettlement.setSupplierId(supplierOrderGroupVo.getSupplierId());
            purchaseSettlement.setTotalAmount(supplierOrderGroupVo.getSettlementAmount());
            purchaseSettlement.setOrderCount(supplierOrderGroupVo.getOrderCount());
            purchaseSettlement.setSettlementDate(supplierOrderGroupVo.getMonth());
            purchaseSettlement.setSettlementStatus("0");
            purchaseSettlementMapper.insert(purchaseSettlement);
        });
    }

    @Override
    public List<SupplierOrderGroupVo> groupList() {
        List<SupplierOrderGroupVo> supplierOrderGroup = purchaseSettlementMapper.groupList();
        return supplierOrderGroup;
    }
}
